package com.xinxin.blockchain.util.legacy;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;

/**
 * @author ys
 * @date 2021/5/20
 */
@Slf4j
public class DayuSmsUtil {

    private static final String ACCESS_KEY_ID = "LTAIDD1LYrpWMvLY";
    private static final String ACCESS_KEY_SECRET = "iDpvytE8vRWGUMI5uU4uDpAj8Nxhvp";

    // 签名
    public static final String SIGN = "CAFarm";

    // 验证码国内通用模板英文
    public static final String TEMPLATE_CODE_HOME_EN = "SMS_165676585";
    // 验证码国际通用模板英文
    public static final String TEMPLATE_CODE_INTERNATIONAL_EN = "SMS_165677860";

    // 有效时间
    public static final int EFFECTIVE = 5 * 60;
    // 发送间隔
    public static final int PERIOD = 60;

    public static String sendCode(String sign, String template, String sendMobile, String code) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("SignName", sign);
        request.putQueryParameter("TemplateCode", template);
        request.putQueryParameter("PhoneNumbers", sendMobile);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");

        return client.getCommonResponse(request).getData();
    }
}
